package org.example.jedis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.util.Optional;

/**
 * @author: tianhao.luo@hand-china.com 2021/5/24  10:49
 */
public class JedisDemo {
    public static final Logger LOGGER = LoggerFactory.getLogger(JedisDemo.class);
    public static final String HOST = "192.168.15.79";
    public static final String PORT = "6379";
    public static Jedis jedis;

    static {
        LOGGER.info("======================jedis loading========================");
        // 1. 创建jedis对象
        Jedis jedis = new Jedis(HOST,Integer.parseInt(PORT));
        // 2. 测试连接
        String ping = jedis.ping();
        // 3. 打印redis连接信息
        LOGGER.info("redis连通信息:{},当前连接到的db为:{}",ping, jedis.getDB());
        LOGGER.info("======================jedis success========================");
        JedisDemo.jedis =  jedis;
    }

    /**
     * 清除key value,便于后续的测试
     * @param key 键
     */
    public void delExistsKeyValue(String key){
        if (jedis.exists(key)) {
            LOGGER.info("=======================清除{}开始=============================",key);
            LOGGER.info("当前key:{}的类型为:{}",key,jedis.type(key));
            LOGGER.info("删除key:{},结果为:{}",key,jedis.del(key)==1? "true":"false");
            LOGGER.info("=======================清除{}结束=============================",key);
        }
    }
}
